package com.jinlejia.feedback.idea.ui;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.common.FbApiConfig;
import com.jinlejia.feedback.common.FbDataFilter;
import com.jinlejia.feedback.common.databinding.CommonEvent;
import com.jinlejia.feedback.databinding.FbActivityPutquestionstoBinding;
import com.jinlejia.feedback.idea.ui.bean.FbQuestionOneBean;
import com.weight.CustomToast;
import com.weight.IconToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * @author SongJiaTai
 * @description:提问页面Activity
 * @date :2019/6/25 15:03
 */
public class PutQuestionsToActivity extends AppCompatActivity {
    private FbActivityPutquestionstoBinding binding;
    private List<FbQuestionOneBean> fbQuestionOneBeanlist = new ArrayList<>();
    JSONObject jsonObject = new JSONObject();
    private String question1;
    private String question2;
    private FbProgressDialog progressDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.fb_activity_putquestionsto);
        binding.setCommonEvent(CommonEvent.obtain());
        binding.setEvent(this);
        progressDialog = new FbProgressDialog(this,"请稍后...");
        initData();
    }



    public void questionfenglei(View view){
       ArrayList<Map<String, Object>> data1 = new ArrayList<>();

        for (FbQuestionOneBean fbQuestionOneBean : fbQuestionOneBeanlist) {
            Map<String, Object> map = new HashMap<>();
            map.put("problemTypeName", fbQuestionOneBean.getProblemTypeName());
            map.put("problemTypeCode", fbQuestionOneBean.getProblemTypeCode());
            data1.add(map);
        }

        final QuestionFengleiDialog questionFengleiDialog = new QuestionFengleiDialog(this,data1);
        questionFengleiDialog.setCancelable(true);
        questionFengleiDialog.setOnCheckClickListener(new QuestionFengleiDialog.OnCheckClickListener() {
            @Override
            public void onItemclick(String questionOnew, String questionTwo) {
                question1 = questionOnew;
                question2 = questionTwo;
                binding.questionfenglei.setText("问题分类（必填）"+questionOnew+"-"+questionTwo);
                questionFengleiDialog.dismiss();
            }
        });
        questionFengleiDialog.show();
    }

    public void pushQuestion(View view){
        jsonObject.put("problemTitle",binding.etquestiontitle.getText().toString());
        jsonObject.put("problemContent",binding.etquestionmessage.getText().toString());
        jsonObject.put("problemFirstLevel",question1);
        jsonObject.put("problemSecondLevel",question2);
        if(TextUtils.isEmpty(jsonObject.getString("problemTitle"))){
            CustomToast.showToast(this,"请填写问题标题",1500);
        }else if(TextUtils.isEmpty(jsonObject.getString("problemContent"))){
            CustomToast.showToast(this,"请填写问题内容",1500);
        }else if(TextUtils.isEmpty(jsonObject.getString("problemFirstLevel"))||TextUtils.isEmpty(jsonObject.getString("problemSecondLevel"))){
            CustomToast.showToast(this,"请选择问题分类",1500);
        }else{
            httpPushQuestion();
        }
    }



    /***
     *
     * 网络请求二级选择列表第一列数据
     */
    private void initData() {
        OkHttpUtils.postString()
                   .url(FbApiConfig.FirstLevelClassificationOfProblems)
                   .mediaType(MediaType.parse("application/json; charset=utf-8"))
                   .content(new JSONObject().toString())
                   .build()
                   .execute(new MapCallBack() {
                       @Override
                       public void onError(Call call, Exception e, int id) {
                           CustomToast.showToast(PutQuestionsToActivity.this,"网络错误",1500);
                       }

                       @Override
                       public void onResponse(Map<String, Object> response, int id) {
                           if (showEmptyMessage(response))return;
                           JSONArray dataList = (JSONArray) response.get("data");
                           fbQuestionOneBeanlist = JSON.parseArray(dataList.toString(),FbQuestionOneBean.class);
                       }
                   });
    }

    private boolean showEmptyMessage(Map<String, Object> response){
        if ("501".equals(String.valueOf(response.get("code")))){
            if (response.get("message") != null)
                CustomToast.showToast(this, response.get("message").toString(), 1500);
            return true;
        }
        return false;
    }


    private void httpPushQuestion(){
        progressDialog.show();
        OkHttpUtils.postString()
                .url(FbApiConfig.PushQuestionInformaetion)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (null != progressDialog && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            CustomToast.showToast(PutQuestionsToActivity.this,"网络错误",1500);
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        int code = FbDataFilter.code(response);
                        if (code!=200){
                            if (null != progressDialog && progressDialog.isShowing()) {
                                String message = String.valueOf(response.get("message"));
                                CustomToast.showToast(PutQuestionsToActivity.this,message,1500);
                                progressDialog.dismiss();
                            }
                        }else{
                            if (null != progressDialog && progressDialog.isShowing()) {
                                IconToast.showToast(PutQuestionsToActivity.this, R.mipmap.common_ic_success, "提交成功", 1500);
                                progressDialog.dismiss();
                                finish();
                            }
                        }


                    }
                });
    }

}
